<template>
  <div>
    <div class="accountModel">
      <!-- 标题 -->
      <div class="accountModel-title">选择核算模型</div>


      <!-- 条件筛选 -->
      <div class="-condition">
        <div class="accountModel-condition-form">
          <normal-form-plus
            :formModel="formModel"
            :config="config"
            :plus="plus"/>
        </div>

      </div>
      <div class="accountModel-condition-btn">
        <el-button @click="getAccountingModelByPage()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
        <el-button @click="showWindow = true">新增</el-button>
      </div>


      <!-- 表格-核算报告 -->
      <div class="accountModel-report">
        <normal-table
          :tableData="tableData"
          :paginationConfig="paginationConfig"
          :tableLabel="tableLabel"
          :operationConfig="operationConfig"
          :operationList="operationList"
          @event="handleEvent"
          @changePage="changePage"></normal-table>
      </div>

      <!-- 添加核算模型 -->
      <el-dialog
        title="添加核算模型"
        :visible.sync="showWindow"
        width="30%"
        class="form">
        <!-- form表单 -->
        <div class="formData">
          <normal-form
            :formModel="addFormModel"
            :config="addConfig"
            :plus="plus"/>
        </div>
        <span slot="footer" class="form-footer">
            <el-button type="primary" @click="createAccountingModel()" class="confirm">确 定</el-button>
            <el-button @click="reset()">取 消</el-button>
          </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import NormalTable from '@/components/NormalTable'
import NormalFormPlus from '@/components/NormalFormPlus'
import NormalForm from '@/components/NormalForm'

export default {
  name: 'accountModel',
  components: {
    NormalForm,
    NormalTable,
    NormalFormPlus
  },
  mounted() {
    this.getAccountingModelByPage()
  },
  data() {
    return {
      showWindow: false,
      addConfig: [
        {
          id: '1',
          type: 'input',
          model: 'name',
          name: '模型名称',
          edit: true,
          props: {
            placeholder: '模型名称',
          }
        },
        {
          id: '2',
          type: 'select',
          model: 'method',
          name: '量化方法',
          edit: true,
          props: {
            placeholder: '量化方法',
            options: [
              {value: '0', label: '排放因子法'},
              {value: '1', label: '物料平衡法'}
            ]
          }
        },
        {
          id: '3',
          type: 'input',
          model: 'gwp',
          name: 'GWP信息',
          edit: true,
          props: {
            placeholder: 'GWP信息',
          },
          options: [
            {value: '6', label: '6'},
          ]
        },
        {
          id: '4',
          type: 'text',
          model: 'intro',
          name: '模型简介',
          edit: true,
          props: {
            placeholder: '模型简介',
          }
        },
      ],
      addFormModel: {
        gwp: "",
        intro: "",
        method: "",
        name: ""
      },
      formModel: {
        name: ""
      },
      config: [
        {
          id: 1,
          items: [
            {
              id: '1',
              edit: true,
              type: 'input',
              span: 4,
              model: 'name',
              name: '',
              props: {
                placeholder: "模型名称",
                width: 150
              }
            },
          ]
        },
      ],
      plus: {},
      tableData: [],
      paginationConfig: {
        total: 100,
        page: 1,
        pageSize: 10,
        show: true
      },
      operationList: [
        {
          name: '选择',
          order: 1,
          buttonClass: 'primary',
          plus: {}
        }
      ],
      operationConfig: {select: false},
      tableLabel: [
        {
          prop: "order",
          label: "序号",
          width: 70
        },
        {
          prop: "name",
          label: "模型名称",
        },
        {
          prop: "method",
          label: "量化方法",
          codeTable: {
            0: '排放因子法',
            1: '物料平衡法'
          }
        },
        {
          prop: "intro",
          label: "模型简介",

        },
        {
          prop: "updater",
          label: "更新人",
        },
      ],

    }
  },
  methods: {

    // 重置信息
    reset() {
      this.formModel.name = ""
      this.paginationConfig.page = 1
      this.getAccountingModelByPage()
      this.addFormModel = {
        gwp: "",
        intro: "",
        method: "",
        name: ""
      }
    },
    // 获取核算模型列表
    getAccountingModelByPage() {
      if (this.$MOCK.mock) {
        this.tableData = this.$MOCK.enterprise.accountModelList.data.list
        this.paginationConfig.total = this.$MOCK.enterprise.accountModelList.data.total
        return
      }
      this.$API.enterprise.getAccountingModelByPage({
        name: this.formModel.name == "" ? null : this.formModel.name,
        pageNo: this.paginationConfig.page,
        pageSize: this.paginationConfig.pageSize,
      }).then(response => {
        this.tableData = response.data.list
      })
    },
    // 添加核算模型
    createAccountingModel() {
      this.showWindow = false
      this.$API.enterprise.createAccountingModel({
        ...this.addFormModel
      }).then(response => {
        this.reset()
      })
    },
    // 处理表格点击事件
    handleEvent(order, row) {
      if (order === 1) {
        this.$router.push({path: "/enterpriseCarbonAccounting/emission/account", query: {modelId: row.id}})
      }
    },
    // 分页点击事件
    changePage() {

    }
  }
}
</script>

<style lang="less" scoped>
.accountModel {

  height: 680px;
  overflow: hidden;
  padding: 15px 10px;
  // 标题
  .accountModel-title {
    font-size: 16px;
    font-weight: bold;
  }

  .accountModel-condition {
    display: flex;

    .accountModel-condition-form {
      flex: 3;
    }

    .accountModel-condition-btn {
      flex: 1;
    }
  }

  // 核算报告表格
  .accountModel-report {
    .accountModel-history-title {
      font-size: 14px;
    }
  }
}
</style>
